import {myFetch} from '@/assets/js/utils'
import pingpp from 'pingpp-js'

export default{
	// 购物车结算
	async settleCartGoods(goods_id){
		const data = await myFetch({
			name: 'shopping.sys.settle.account',
			user_id: 0,
			goods_id
		})
		return data
	},
	// 获取邮费
	async getPostage({selectedGoodsId, addressId}){
		const data = await myFetch({
			name: 'shopping.sys.get.postage',
			goods_id: selectedGoodsId,
			address_id: addressId,
			user_id: 0,
			scores_used: 0
		})
		return data
	},
	// 提交订单
	async submitOrder({address_id, goods_id, user_id, note}) {
		const data = await myFetch({
			name: 'shopping.sys.submit.order',
			address_id,
			goods_id,
			is_selected: 0,
			note,
			user_id,
			idcard: '',
			real_name: ''
		})
		return data
	},
	// 提交订单号 获取charge
	async getCharge({type, orderFormLogId, orderId, totalPrice, userId}) {
		// type为1时是刚结算产品 type为2时是付待付款的产品
		const description = type == 1 ? {
			type: 1,
            add_scores: 0,
            reduce_scores: 0,
            user_id: userId,
            order_form_log_id: orderFormLogId
		} : {
			type: 2,
            add_scores: 0,
            reduce_scores: 0,
            user_id: userId,
            order_id: orderId
		}
		const data = await myFetch({
			name: 'shopping.sys.goods.pay',
            channel: 'wx_pub',
            amount: totalPrice * 100,
            order_no: Math.ceil(Math.random()*100) + Date.parse(new Date()),
            description: JSON.stringify(description)
		})
		return data
	},
	// 调用pingpp支付
	WxPay(charge) {
		return new Promise((res, reject) => {
			pingpp.createPayment(charge, (result, err) => {
				// alert('result:' + JSON.stringify(result) + 'err:' + JSON.stringify(err));
				if (result == "success") res()
				else reject()
			})	
		})
	}
}